/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/23
 */

import Vue from 'vue';
import cookie from '../../../grsc-base/util/cookie';
import parseUrl from '../../../grsc-base/util/parseUrl';
import commonService from '../../common/commonService';
import lib from '../../common/lib';
import Loading from '../../../grsc-base/component/Loading.vue';
import WebBox from '../../component/WebBox/WebBox';
import Validator from '../../../grsc-base/component/Validator/main';
import {REG_EMAIL, REG_LOGPWD, REG_PAYPWD} from '../../../grsc-base/common/constants';

import tpl from './tpl.html';
import service from './service';

let query = parseUrl();
let inviteCode = '';
if (query.r) {
    inviteCode = query.r;
}

if (inviteCode) {
    lib.isGoMobilePage('m_register.html?r=' + inviteCode);
} else {
    lib.isGoMobilePage('m_register.html');
}

const isNeedStat = lib.isNeedStat();

// if(lib.isMobile() && window.location.href.indexOf('m_register.html') < 0){
//     // 移动端跳转
//     window.location.href = 'm_register.html';
// }

export default Vue.extend({
    template: tpl,
    components: {
        Loading,
        Validator,
        WebBox
    },
    data() {
        return {
            lib: lib,
            hasAgree: false,
            form: {
                email: '',
                setPsd: '',
                confirmNewPwd:'',
                inviteCode: inviteCode
            },
            rules: {
                email: {
                    tip: "",
                    required: true,
                    requiredTip: this.$t('common.emailRequireTip'),
                    pattern: REG_EMAIL,
                    patternTip: window.CODE.EMAIL[this.$i18n.locale]
                },
                setPsd: {
                    required: true,
                    requiredTip: this.$t('common.loginPassRequireTip'),
                    minlength: 6,
                    minlengthTip: this.$t('common.ruleLoginPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleLoginPassLength'),
                    pattern: REG_LOGPWD,
                    patternTip: this.$t('common.ruleLoginPassPattern')
                },
                confirmNewPwd: {
                    required: true,
                    requiredTip: this.$t('common.repeatPassRequireTip'),
                    equalTo: 'setPsd',
                    equalToTip: this.$t('common.passEqualTip')
                }
            },
            isLoading: true,
            isRequesting: false
        }
    },
    created() {

    },
    watch: {},
    mounted(){
        // ued体验优化
        setTimeout(()=>{
            this.isLoading = false;
        },500)
    },
    methods: {

        changeAgree($event) {
            if ($event.target.checked === true) {
                this.hasAgree = true;
            } else {
                this.hasAgree = false;
            }
        },

        /**
         * 验证注册密码
         */
        checkPsd(){
            this.rules.setPsd.tip = this.$t('common.ruleLoginPassLength');
            if(isNeedStat){
                lib.stat(['_trackEvent', '注册', '第一次输入密码Input获取焦点']);
            }
        },
        checkPsdBlur() {
            this.rules.setPsd.tip = '';
        },

        // 邮箱
        statEmail(){
            if(isNeedStat){
                lib.stat(['_trackEvent', '注册', '邮箱Input获取焦点']);
            }
        },

        // 再次确定
        reCheckPsd(){
            if(isNeedStat){
                lib.stat(['_trackEvent', '注册', '再次输入密码Input获取焦点']);
            }
        },

        /**
         * 注册验证
         */
        submit() {
            if (this.rules.$checkAll() === false) {
                return;
            }

            let params = {
                loginPassword: this.form.setPsd,
                lang:'/'+this.$i18n.locale
            };

            if ($.trim(this.form.inviteCode)) {
                params.inviteCode = this.form.inviteCode;
            }

            let header = {
                'account-no': this.form.email,
            };

            this.isRegisting = true;
            service.preRegister(params, header).done((rep)=> {
                if(isNeedStat){
                    lib.stat(['_trackEvent', '注册', '立即注册点击']);
                }
                if (rep.code === window.CODE.SUCCESS) {
                    // lib.doLogin(rep.data.token, this.form.email, false);

                    this.$router.push({
                        path: '/confirm',
                        params: { account: this.form.email }
                    });

                    // 注册代表进行了登录
                    // cookie.set('LOGIN_TOKEN', 1 ,{path:'/'});
                    // window.localStorage.setItem('account-no', this.form.email);
                } else {
                    this.isRegisting = false;
                    if (rep.code === window.CODE.HAS_REGISTER.CODE) {
                        this.rules.email.$setError(window.CODE.HAS_REGISTER[this.$i18n.locale]);
                    } else if (rep.code === window.CODE.REGISTER_MORE_TIMES.CODE) {
                        window.error(window.CODE.REGISTER_MORE_TIMES[this.$i18n.locale]);
                    } else {
                        window.error(rep.msg);
                    }
                }
            });
        },
        goToLogin(){
            if(isNeedStat){
                lib.stat(['_trackEvent', '注册', '立即注册点击'],()=>{
                    window.location.href = './login.html';
                });
            }
            
        }
    }
});










